package com.prs.crm.dao.doc.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.courser.Util;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.doc.ShareDeptDocDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.doc.DeptDoc;
import com.prs.crm.domain.doc.ShareDeptDoc;
import com.prs.crm.domain.hr.Person;

public class ShareDeptDocDaoImp extends BaseDaoImpl<ShareDeptDoc> implements ShareDeptDocDao {

	@SuppressWarnings("unchecked")
	@Override
	public Collection<ShareDeptDoc> getShareDeptDoc(final DeptDoc deptDoc) {
		// TODO Auto-generated method stub
		return (Collection<ShareDeptDoc>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(ShareDeptDoc.class);
				criteria.add(Restrictions.eq("deptDoc", deptDoc));
				return criteria.list();
			}});
	}

	@Override
	public void deleteDeptDoc(DeptDoc deptDoc) {
		// TODO Auto-generated method stub
		Query query = this.getSession().createQuery("delete from ShareDeptDoc where deptDoc=?");
		query.setParameter(0, deptDoc);
		query.executeUpdate();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<ShareDeptDoc> getValidIssuePayroll(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<ShareDeptDoc>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(ShareDeptDoc.class);
				criteria.add(Restrictions.eq("person", person));
				criteria.add(Restrictions.le("beginTime", Util.nowFullTime()));
				criteria.add(Restrictions.ge("endTime", Util.nowFullTime()));
				return criteria.list();
			}
			
		});
	}

}
